package com.example.demo.Service;

import com.example.demo.entity.Comment;
import com.example.demo.entity.Post;
import com.example.demo.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {
    @Autowired
    private CommentRepository commentRepository;

    //创建或更新评论
    public Comment save(Comment comment) {
        return commentRepository.save(comment);
    }

    //获取文章下的所有评论
    public List<Comment> findAll() {
        return commentRepository.findAll();
    }

    //通过Post查找评论
    public List<Comment> findCommentsByPost(Post post) {
        return commentRepository.findByPost(post);
    }

    //通过Username查找评论
    public List<Comment> findCommentsByUser(String username) {
        return commentRepository.findByUser_Username(username);
    }

    //通过Id查找评论
    public List<Comment> findCommentsByUserId(Long userId) {
        return commentRepository.findByUser_Id(userId);
    }
    //删除评论
    public void deleteById(Long id) {
        commentRepository.deleteById(id);
    }
}
